package com.edu.framework.websocket.handler;

import cn.hutool.core.util.StrUtil;
import com.edu.framework.websocket.WebSocketHolder;
import com.edu.framework.websocket.message.base.Response;
import com.edu.framework.websocket.message.studyscore.StudyScoreRequest;
import com.edu.framework.websocket.message.studyscore.StudyScoreVO;
import com.edu.framework.websocket.service.StudyScoreService;
import com.edu.system.domain.Student;
import com.edu.system.service.IStudentService;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Component
public class StudyScoreRequestHandler implements IHandler<StudyScoreRequest> {

    @Resource
    private StudyScoreService wsStudyScoreService;

    @Resource
    private IStudentService iStudentService;

    @Override
    public void execute(WebSocketSession session, StudyScoreRequest message) {
        if (message == null) {
            return;
        }
        Map<String, String> data = message.getData();
        String cardID = data.get(StudyScoreRequest.CARD_ID);
        String year = data.get(StudyScoreRequest.YEAR);
        String month = data.get(StudyScoreRequest.MONTH);
        Response response = null;

        if (StrUtil.isBlank(cardID)){
            response = Response.fail(StudyScoreRequest.TYPE, "卡id不能为空");
        } else {
            Student student = iStudentService.selectStudentByCardNo(cardID);

            List<StudyScoreVO> collect = wsStudyScoreService.getScoreListVOS(student.getId(), year, month);
            response = Response.ok(StudyScoreRequest.TYPE, StudyScoreRequest.RESP_KEY, collect);
        }

        try {
            WebSocketHolder.send(session,response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getType() {
        return StudyScoreRequest.TYPE;
    }




}
